package _2022.hot100._200_岛屿数量;

/**
 * @author： YHSimon
 * @date： 2022-05-10 14:11
 */
public class Solution {
    public int numIslands(char[][] grid){
        if(grid==null||grid.length==0){
            return 0;
        }
        int ans=0;
        int r=grid.length;
        int c=grid[0].length;
        for(int i=0;i<r;i++){
            for(int j=0;j<c;j++){
                if(grid[i][j]=='1'){
                    ans++;
                    dfs(grid,i,j);
                }
            }
        }
        return ans;
    }

    private void dfs(char[][] grid, int i, int j) {
        int r=grid.length;
        int c=grid[0].length;
        if(i<0||j<0||i>=r||j>=c||grid[i][j]=='0'){
            return;
        }
        grid[i][j]='0';
        dfs(grid, i-1, j);
        dfs(grid, i+1, j);
        dfs(grid, i, j-1);
        dfs(grid, i, j+1);
    }


    public static void main(String[] args) {
        Solution s = new Solution();
        System.out.println(s.numIslands(new char[][]{
                {'1','1','1','1','0'},
                {'1','1','0','1','0'},
                {'1','1','0','0','0'},
                {'0','0','0','0','0'}
        }));
    }
}
